package com.itic.ht.mem.distribution.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.common.constants.IticConst;
import com.itic.appbase.framework.common.constants.ResponseCode;
import com.itic.appbase.framework.common.controller.BaseController;
import com.itic.appbase.framework.exception.definition.ParameterErrorException;
import com.itic.ht.mem.distribution.persistence.model.Cate;
import com.itic.ht.mem.distribution.persistence.model.CateExample;
import com.itic.ht.mem.distribution.service.CateService;

@Controller
@RequestMapping("/mem/distribution")
public class CateController extends BaseController {
	
	@Autowired
	private CateService cateService;
	
	//////////////////////////////////////////////////////常量区////////////////////////////////////////////////////////////////
	private static final String MODEL_NAME_CHINA = "分销费率";//中文名
	private static final String MODEL_NAME＿ENGLISH = "member";//英文名
	private static final String PAGE_COMMON_PATH = "/mem/distribution/distribution";//通用路径
	private static final String COMMON_URL = "/mem/distribution/";//通用路径
	
	
	/*
     * 主页面
     */
    @RequestMapping(value="/init")
    public String init(Model model){
    	model.addAttribute("name_china", MODEL_NAME_CHINA);
    	model.addAttribute("name_english", MODEL_NAME＿ENGLISH);
    	model.addAttribute("common_url", COMMON_URL);
    	return PAGE_COMMON_PATH+"Main";
    }
    
    /*
     * 编辑页面
     */
    @RequestMapping(value="/initEdit")
    public String initEdit(String id , Model model){
    	if( null != id ) {
    		Cate temp = cateService.get(id);
    		model.addAttribute("editData",iticJsonMapper.toJson(temp));
    		model.addAttribute("name_china", MODEL_NAME_CHINA);
        	model.addAttribute("name_english", MODEL_NAME＿ENGLISH);
        	model.addAttribute("common_url", COMMON_URL);
    	}else {
            throw new ParameterErrorException();
        }
    	return PAGE_COMMON_PATH+"Edit";
    }
    
    @RequestMapping(value = "/list")
    @ResponseBody
    public Map<String, Object> list(){
        // 声明返回对象
        Map<String, Object> resultMap = new HashMap<String, Object>();
        //创建查询条件
        CateExample example = new CateExample();
        example.createCriteria().andIsEffectiveEqualTo(DBConst.TRUE);
        // 获取数据
        List<Cate> list = cateService.list(example);
        resultMap.put(IticConst.RESULT, ResponseCode.SUCCESS.getCode());
        resultMap.put(IticConst.DATA, list);
        return resultMap;

    }
    
    /**
     * 编辑保存
     * @param templet
     * @return
     */
    @RequestMapping(value="/edit")
    @ResponseBody
    public Map<String,String> edit( Cate temp){
    
    	
    	 Map<String, String> resultMap = new HashMap<String, String>();
         String result = ResponseCode.SERVER_ERROR.getCode();
         result = cateService.edit(temp);
         resultMap.put(IticConst.RESULT, result);
         return resultMap;
    }
	
}
